package com.bwie.parkdemo.mvvm.viewmodel;

import android.app.Application;
import android.databinding.ObservableField;
import android.support.annotation.NonNull;
import android.view.View;

import com.bwie.lib_core.base.BaseEntity;
import com.bwie.lib_core.mvvm.base.BaseOBViewModel;
import com.bwie.parkdemo.mvvm.model.DepartmentModel;
import com.bwie.parkdemo.mvvm.model.entity.AppointDepartmentEntity;
import com.bwie.parkdemo.mvvm.model.entity.DepartmentEntity;
import com.bwie.parkdemo.mvvm.model.entity.DepartmentUserEntity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 部门对应ViewModel
 * */
public class DepartmentViewModel extends BaseOBViewModel<DepartmentModel>{

    private int type = 0;//不同的fragmetn中区分请求那个接口0已分组1未分组
    public int adapterType = 4;//适配器类型
    public ItemListener listener;//item点击事件
    public ObservableField<List<Object>>datas;//关联到适配器上的数据源
    private DepartmentUserEntity.Values values;
    private List<Object>departmentDatas;
    private int userId;
    private int index;

    public DepartmentViewModel(@NonNull Application application) {
        super(application);
        listener = new ItemListener();
        datas = new ObservableField<>();
    }

    public void setType(int type) {
        this.type = type;
    }

    @Override
    protected DepartmentModel createModel() {
        return new DepartmentModel();
    }

    @Override
    protected void initData() {
        userId = m.requestUser(getApplication()).uId;
        Observable<BaseEntity> observable;
        if (type == 0){
            //已分组请求
            observable = m.request(null);
        }else {
            //未分组请求
            observable = m.requestUnDepartment();
        }
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    public void refreshDatas(){
        userId = m.requestUser(getApplication()).uId;
        Observable<BaseEntity> observable = null;
        //已分组请求
        observable = m.request(null);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    @Override
    public void onNext(BaseEntity baseEntity) {
        if (baseEntity instanceof DepartmentUserEntity) {
            DepartmentUserEntity entity = (DepartmentUserEntity) baseEntity;
            datas.set(entity.getDepartmentUserValues());
        }else if (baseEntity instanceof DepartmentEntity){
            //获取部门数据
            DepartmentEntity entity = (DepartmentEntity) baseEntity;
            departmentDatas = entity.getDepartmentValues();
            EventBus.getDefault().post(departmentDatas);
        }else {
            AppointDepartmentEntity entity = (AppointDepartmentEntity) baseEntity;
            //接受到指定成功,移除当前未分组数据源,刷新已分组数据源
            if (entity.statuesCode.equals("200")){
                showMsg(entity.msg);
                List<Object>list = new ArrayList<>();
                list.addAll(datas.get());
                list.remove(index);
                datas.set(list);
                datas.notifyChange();
            }
        }
    }

    //发起指定部门请求
    public void appointDepartment(int index){
        DepartmentEntity.Values entity = (DepartmentEntity.Values) departmentDatas.get(index);
        Map<String,Object>map = new HashMap<>();
        map.put("dept_id",entity.departmentId);
        map.put("uId",values.uId);
        String jsonStr = new Gson().toJson(map);
        RequestBody body = RequestBody.create(MediaType.parse("application/json"),jsonStr);
        m.requestAppointDepartment(body).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    public class ItemListener implements BaseQuickAdapter.OnItemClickListener{

        @Override
        public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
            //未分组列表点击事件触发->点击弹出全部部门
            if (type == 0)
                return;
            index = position;
            //弹出popupwindow显示全部部门列表
            //给对应被点击用户选择部门->向undepartmentfragment发eventbus事件
            values = (DepartmentUserEntity.Values) adapter.getItem(position);
            //发起获取全部部门列表接口
            Map<String,Object>map = new HashMap<>();
            map.put("departmentUserId",userId);
            String jsonStr = new Gson().toJson(map);
            RequestBody body = RequestBody.create(MediaType.parse("application/json"),jsonStr);
            m.requestDepartment(body).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(DepartmentViewModel.this);
        }
    }

}
